<?php
class App {

    public function __construct() {
        $this->define();
        $this->normalizeRequest();
        $this->init();
    }


    private function init() {
        require_once APP_PATH . '/system/core/function.php';

        spl_autoload_register( array( $this, 'autoload' ) );

        // 检查PHP环境
        if ( substr( PHP_VERSION, 0, 3 ) < 5.2 ) {
            mc_error( '本系统运行环境要求PHP 5.2！' );
        }

        // 检查配置文件
        if ( !is_readable( APP_PATH . '/system/config.php' ) ) {
            if ( 'install' != APP_MODULE ) {
                header( 'Location: ' . mc_blog_url() . '/install/' );
                exit;
            }
        } else {
            require_once APP_PATH . '/system/config.php'; // 加载配置文件
        }

        $this->mc_path_info_route();
    }

    // 在使用PATH_INFO的情况下，对路由进行预处理
    private function mc_path_info_route() {
        $rewrite = intval( mc_setting( 'rewrite' ) );
        if ( $rewrite != 1 ) return;
        $url_args = explode( "/", $_SERVER[ 'PATH_INFO' ] );
        $url_sort = array();
        for ( $u = 1; $u < count( $url_args ); $u++ ) {
            if ( $u == 1 ) {
                $url_sort[ 'c' ] = $url_args[ $u ];
            } elseif ( $u == 2 ) {
                $url_sort[ 'a' ] = $url_args[ $u ];
            } else {
                $url_sort[ $url_args[ $u ] ] = isset( $url_args[ $u + 1 ] ) ? $url_args[ $u + 1 ] : "";
                $u += 1;
            }
        }
        if ( "POST" == strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) ) {
            $_REQUEST = $_POST = $_POST + $url_sort;
        } else {
            $_REQUEST = $_GET = $_GET + $url_sort;
        }
    }

    public function run() {

        if ( !defined( 'APP_MODULE' ) ) {
            mc_error( '模块未定义!' );
        }

        $__controller = mc_request( 'c', 'main' );
        $__action = mc_request( 'a', 'index' );

        // 对路由进行自动执行相关操作
        if ( $launch = mc_launch( 'route' ) ) {
            list( $__controller, $__action ) = $launch;
        }

        // 对将要访问的控制器类进行实例化
        $handle = mc_class( $__controller, null, APP_PATH . '/system/' . APP_MODULE . "/{$__controller}.php" );

        // 调用控制器出错将调用路由错误处理函数
        if ( !is_object( $handle ) || !method_exists( $handle, $__action ) ) {
            mc_error( "请检查控制器目录下是否存在该控制器{$__controller}/动作{$__action}。" );
        }

        if ( method_exists( $handle, 'init' ) ) {
            $handle->init();
        }

        // 路由并执行用户代码
        $handle->$__action( $__controller, $__action );
    }

    private function parseUrl() {

    }

    private function autoload( $class_name ) {
        mc_import( strtolower( $class_name . '.php' ) );
    }

    private function define() {
        define( 'APP_CACHE', APP_PATH . '/system/cache/' );
        define( 'APP_TPL', APP_PATH . '/system/tpl/' );
        define( 'APP_PLUGIN', APP_PATH . '/system/plugin/' );
        define( 'APP_LOG', APP_PATH . '/system/logs/' );
        define( 'APP_VERSION', '1.0' );
    }

    private function normalizeRequest() {
        if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
            if ( isset( $_GET ) ) {
                $_GET = $this->dostripslashes( $_GET );
            }
            if ( isset( $_POST ) ) {
                $_POST = $this->dostripslashes( $_POST );
            }
            if ( isset( $_REQUEST ) ) {
                $_REQUEST = $this->dostripslashes( $_REQUEST );
            }
            if ( isset( $_COOKIE ) ) {
                $_COOKIE = $this->dostripslashes( $_COOKIE );
            }
        }

        header( 'Content-Type: text/html; charset=UTF-8' );
        date_default_timezone_set( 'PRC' );
        ini_set( "display_errors", "On" );
        ob_start();
        session_start();
        error_reporting( E_ALL );
    }

    private function dostripslashes( $value ) {
        $value = is_array( $value ) ? array_map( array( $this, 'dostripslashes' ), $value ) : stripslashes( $value );
        return $value;
    }
}